package cn.js.handler;

import cn.js.common.R;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * 匿名用户访问无权限资源的处理类
 */
@Component
public class AnonymousAuthenticationHandler implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse
            response, AuthenticationException authException) throws IOException, ServletException {
        //设置客户端的响应的内容类型
        response.setContentType("application/json;charset=UTF-8");
        String result = null;
        //获取输出流
        ServletOutputStream outputStream = response.getOutputStream();
        // System.out.println("异常消息:"+authException.getMessage()+",对象:"+authException);
        if (authException instanceof BadCredentialsException) {
            // 用户名未找到，可以在这里添加自定义处理逻辑
            result = JSON.toJSONString(R.error()
                            .code(HttpServletResponse.SC_UNAUTHORIZED)
                            .message(authException.getMessage()),
                    SerializerFeature.DisableCircularReferenceDetect);
        } else if (authException instanceof
                InternalAuthenticationServiceException) {
            result = JSON.toJSONString(R.error()
                            .code(HttpServletResponse.SC_UNAUTHORIZED)
                            .message("用户名为空!"),
                    SerializerFeature.DisableCircularReferenceDetect);
        } else {
            // 其他身份验证异常处理
            result = JSON.toJSONString(R.error()
                            .code(600).message("匿名用户无权限访问！"),
                    SerializerFeature.DisableCircularReferenceDetect); //消除循环引用
        }
        outputStream.write(result.getBytes(StandardCharsets.UTF_8));
        outputStream.flush();
        outputStream.close();
    }
}
